package DB;
import java.util.Iterator;
import java.util.Vector;

import Domain.forumManager.UserManager;

import objects.Frm;
import objects.Message;
import objects.Trd;
import objects.User;

public class DB {
	
	//the forum data
	
	
	Vector <Frm> _forum;
	Vector<User> users;
	
	
	public DB()
	{
		users=new Vector<User>();
		User admin = new User("manager");
		admin.setAsManager();
		users.add(admin);
		_forum =new Vector<Frm>();
	}
	
	
	public Frm findFroum(String nameOfForum)
	{
		for (Iterator iterator = this._forum.iterator(); iterator.hasNext();) {
			Frm f = (Frm) iterator.next();
			if (f.getNameOfFourm().equals(nameOfForum))
			{
				return f;
			}
			
		}
		return null;
	}

	
	
	/**
	 * add new forum 
	 * @param nameOfForum the Name of the forum
	 */

	public Frm addForum(String nameOfForum)
	{
		Frm f=new Frm(nameOfForum);
		this._forum.add(f);
		return f;
	}
	
	public Trd addThreadToFroum(String nameOfForum,String nameOfThread)
	{
		
		Frm f=this.findFroum(nameOfForum);
		if (f==null)
		{
			return null;
		}
		return f.addThread(nameOfThread);
		
	}
	


	/**
	 * 
	 * @param nameOfForum
	 * @param nameOfThread
	 * @param msg
	 * @return the user that owns the message
	 */
	public User findOwnerOfMessage(String nameOfForum ,String nameOfThread,String msg)
	{
		Frm f=this.findFroum(nameOfForum);
		if (f==null)
			return null;
		Vector <Message> msgs =f.getMessageFromThread(nameOfThread);
		if (msgs==null)
			return null;
		for (Iterator iterator = msgs.iterator(); iterator.hasNext();) {
			Message message = (Message) iterator.next();
			if(message.getTitleOfMessage().trim().equals(msg.trim()))
			{
				return message.getOwner();
			}
			
		}
		return null;
		
	
	}
	/**
	 * return the messages of thread in spasific forum
	 * @param nameOfForum the name of the form that the tread is located
	 * @param nameOfThread the name of the tread we want to get the message from
	 * @return
	 */
	public Vector <Message> getMessageFromThread (String nameOfForum,String nameOfThread)
	{
		return findFroum(nameOfForum).getMessageFromThread(nameOfThread);
	}
	/**
	 * @return the threads
	 */
	public Vector<Trd> getThreads(Frm f) {
		return f.getThreads();
	}
	public Vector<Trd> getThreads(String nameOfForum)
	{
		return this.findFroum(nameOfForum).getThreads();
	}


	/**
	 * @return the _forum
	 */
	public Vector<Frm> get_forum() {
		return _forum;
	}
	
	//the user data
	
	

	/**
	 * add User u to the User database
	 * @param u the User we want to be added
	 */
	public void addUser(User u)
	{
		this.users.add(u);
	}

	/**
	 * remove user u from the database 
	 * All the pepole that are friend of the user will delete him from their friend 
	 * @param u
	 */
	public void removeUser (User u)
	{
		this.users.remove(u);

	}
	/**
	 * 
	 * @param nameOfUser the name of the user wanted to be found
	 * @return user with the name nameOfUser
	 */
	public User findUser(String nameOfUser)
	{
		for (Iterator iterator = this.users.iterator(); iterator.hasNext();) {
			User user = (User) iterator.next();
			if(user.get_nameOfUser().equalsIgnoreCase(nameOfUser))
			{
				
				return user;
			}
		}
		return null;
	}
	
	
	




	/**
	 * @return the users vector
	 */
	public Vector<User> getUsers() {
		Vector <User> u=new Vector<User>(users);
		return u;
	}

	
}
